import React from 'react'
import { Card, Form, Input, Checkbox, Button, message } from 'antd'
import logo from '../../assets/logo.jpg'
import './index.scss'
import { useStore } from '@/store'
import { useNavigate } from 'react-router-dom'
// Form.Item的简写形式
const Item = Form.Item

// 点击登录按钮时触发 参数values即是表单输入数据

const Login = () => {
  const navigate = useNavigate()
  const { loginStore } = useStore()
  //测试默认密码为246810
  const onFinish = async values => {
    const { mobile, code } = values
    console.log('我拿到请求数据辣', mobile, code)
    // console.log(values)
    try {
      console.log('我拿到仓库数据辣', loginStore)
      const res = await loginStore.login({ mobile, code })
      // console.log(res)
      navigate('/')
    } catch (e) {
      message.error(e.response?.data?.message || '登陆失败')
    }
  }

  return (
    <div className='login'>
      <Card className='login-container'>
        <img src={logo} alt="图片" className='login-logo' />
        {/* 登录表单 */}
        <Form
          validateTrigger={['onBlur', 'onChange']}
          initialValues={{
            mobile: '13911111111',
            code: '246810',
            remember: true,
          }}
          onFinish={onFinish}
        >
          <Item
            name='mobile'
            rules={[
              { required: true, message: '请输入手机号' },
              {
                pattern: /^1[3-9]\d{9}$/, // 设置正则匹配规则
                validateTrigger: 'onBlur', // 设置触发时机失去焦点时触发
                message: '请输入正确的手机号格式',
              },
            ]}
          >
            <Input size='large' placeholder='请输入手机号' />
          </Item>
          <Item
            name='code'
            rules={[
              { required: true, message: '请输入密码' },
              {
                len: 6, // 设置密码长度为6位数
                validateTrigger: 'onBlur',
                message: '请输入6位密码',
              },
            ]}
          >
            <Input.Password size='large' placeholder='请输入密码' />
          </Item>
          <Item name='remember' valuePropName='checked'>
            <Checkbox className='login-checkbox-label'>
              我已经阅读并同意 [用户协议] 和 [隐私条款]
            </Checkbox>
          </Item>
          <Item>
            <Button type='primary' htmlType='submit' size='large' block>
              登录
            </Button>
          </Item>
        </Form>
      </Card>
    </div>
  );

}

export default Login
